package com.ruoyi.web.article.controller;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.article.domain.TrainArticle;
import com.ruoyi.web.article.service.ITrainArticleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文章Controller
 *
 * @author 范书铭
 * @date 2024-10-13
 */
@Api("文章")
@RestController
@RequestMapping("/article/article")
public class TrainArticleController extends BaseController {
    @Autowired
    private ITrainArticleService trainArticleService;

/**
 * 查询文章列表
 */
@ApiOperation("查询文章列表")
@PreAuthorize("@ss.hasPermi('article:article:list')")
@GetMapping("/list")
    public TableDataInfo list(TrainArticle trainArticle) {
        startPage();
        List<TrainArticle> list = trainArticleService.selectTrainArticleList(trainArticle);
        return getDataTable(list);
    }

    /**
     * 导出文章列表
     */
    @ApiOperation("导出文章列表")
    @PreAuthorize("@ss.hasPermi('article:article:export')")
    @Log(title = "文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TrainArticle trainArticle) {
        List<TrainArticle> list = trainArticleService.selectTrainArticleList(trainArticle);
        ExcelUtil<TrainArticle> util = new ExcelUtil<TrainArticle>(TrainArticle. class);
        util.exportExcel(response, list, "文章数据");
    }

    /**
     * 获取文章详细信息
     */
    @ApiOperation("获取文章详细信息")
    @PreAuthorize("@ss.hasPermi('article:article:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(trainArticleService.getArticleById(id, SecurityUtils.getUserId()));
    }

    /**
     * 新增文章
     */
    @ApiOperation("新增文章")
    @PreAuthorize("@ss.hasPermi('article:article:add')")
    @Log(title = "文章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TrainArticle trainArticle) {
        return toAjax(trainArticleService.save(trainArticle));
    }

    /**
     * 修改文章
     */
    @ApiOperation("修改文章")
    @PreAuthorize("@ss.hasPermi('article:article:edit')")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TrainArticle trainArticle) {
        return toAjax(trainArticleService.save(trainArticle));
    }

    /**
     * 删除文章
     */
    @ApiOperation("删除文章")
    @PreAuthorize("@ss.hasPermi('article:article:remove')")
    @Log(title = "文章", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(trainArticleService.removeByIds(Arrays.asList(ids)));
    }

    @ApiOperation("查个人阅读历史记录")
    @PreAuthorize("@ss.hasPermi('article:article:userlist')")
    @GetMapping("/userHistory")
    public TableDataInfo userHistory() {
        startPage();
        List<TrainArticle> list = trainArticleService.selectUserHistory(SecurityUtils.getUserId());
        return getDataTable(list);
    }

    @ApiOperation("查询个人收藏的文章")
    @PreAuthorize("@ss.hasPermi('article:article:userlist')")
    @GetMapping("/userCollectionArticle")
    public TableDataInfo userCollectionArticle() {
        startPage();
        List<TrainArticle> list = trainArticleService.userCollectionArticle(SecurityUtils.getUserId());
        return getDataTable(list);
    }

    @ApiOperation("查文章阅读历史记录")
    @PreAuthorize("@ss.hasPermi('article:article:list')")
    @GetMapping("/articleHistory")
    public TableDataInfo articleHistory() {
        startPage();
        List<SysUser> list = trainArticleService.selectArticleHistory(SecurityUtils.getUserId());
        return getDataTable(list);
    }
}
